//index.js
//获取应用实例
const app = getApp()
Page({
    data: {
        currentPage: 1,
        videoArr: [],
        id:1,
        type: 3,
        desc:null,
        videoUrl: {},
        userInfo: {},
        hasUserInfo: false,
        showAuth: false,
        use_video_id:null,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        uploadToken:'',
        isLogin: false,
        totalPage:null,
        like_num:0
    },
    onLoad: function () {
        
    },
    /**
 * 用户点击右上角分享
 */
    onShareAppMessage: function () {
        let that = this
        let path = '/pages/index/index'
        return {
            title: '学而思国际演讲',
            path: path,
            success: function (res) {
            }
        }
    },
    onShow(){
        this.getauth = this.selectComponent('#getauth')
        let sign = wx.getStorageSync('sign')
        let that = this
        if(!sign){
            app.wxLogin(jscode => {
                app.globalData.jscode = jscode
                wx.getSetting({
                    success(settingRes) {
                        if (!settingRes.authSetting['scope.userInfo']) {
                            that.getauth.showAuth()
                        } else {
                            // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                            wx.getUserInfo({
                                withCredentials: true,
                                success: function (getUserInfoRes) {
                                    app.globalData.userInfo = getUserInfoRes.userInfo
                                    app.globalData.encryptedData = getUserInfoRes.encryptedData
                                    app.globalData.iv = getUserInfoRes.iv
                                }
                            })
                        }
                    }
                })
            })
        }else{
            app.getUploadToken()
            this.setData({
                isLogin: true
            })
        }
        this.setData({
            currentPage: 1,
            videoArr: []
        })
        this.getActiveIofo()
    },
    //登录成功回调
    _loginSucess(){
        this.setData({
            isLogin: true
        })
        app.getUploadToken()
    },
    // 获取我的演讲信息
    getActiveIofo:function() {
        let that = this;
        let sign = wx.getStorageSync('sign') || ''
        let data = {
            sign: sign,
            aid: app.globalData.activity_id
        }
        app.ajax({
            data: data,
            url: 'Videolike/getMyActivityInfo',
            method: 'GET'
        }).then(response => {
            if (response.data.code == 200) {
                let resData = response.data.data;
                app.globalData.user_video_id = resData.videoInfo.use_video_id
                app.globalData.activity_id = resData.aid
                if (resData.videoInfo.like_num && resData.videoInfo.like_num > 1000) {
                    resData.videoInfo.like_num = app.numChange(resData.videoInfo.like_num)
                }
                that.setData({
                    desc: resData.desc,
                    videoUrl: resData.videoInfo || [],
                    use_video_id: resData.videoInfo.use_video_id || ''
                })
                this.getVideolikeList(this.data.currentPage)
            } else {
                wx.showModal({
                    title: '温馨提示',
                    content: response.data.message,
                    showCancel: false
                })
            }
        })
    },
    // 上传视频
    chooseVideo:function() {
        let sign = wx.getStorageSync('sign')
        let that = this
        if (sign) {
            app.chooseVideo(() =>{
                that.getActiveIofo()
            })
        } else {
            wx.navigateTo({
                url: '/pages/userLogin/userLogin',
            })
        }
        
    },
    // 登录
    login:function() {
        wx.navigateTo({
            url: '/pages/userLogin/userLogin',
        })
    },
    //授权手机号
    getPhoneNumber(e) {
        var that = this
        if (e.detail.encryptedData) {
            wx.request({
                url: app.globalData.url + 'index/videoLikeMiniAuthLogin',
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                    jscode: app.globalData.jscode,
                    encryptedData: e.detail.encryptedData,
                    iv: e.detail.iv,
                    type: 5
                },
                success: function(msg) {
                    console.log(msg)
                },
                fail: function() {
                    wx.showModal({
                        title: '提示',
                        content: '注册失败，请稍后再试',
                        showCancel: false
                    })
                }
            })
        }
    },   
    getUserInfo: function(e) {
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    // 获取视频列表信息
    getVideolikeList:function(cupage) {
        let sign = wx.getStorageSync('sign') || ''
        let that = this;
        let data = {
            sign: sign,
            page:cupage,
            aid: app.globalData.activity_id
        }
        app.ajax({
            data: data,
            url: 'Videolike/rank',
            method: 'GET'
        }).then(response => {
            if (response.data.code == 200) {
                let resData = response.data.data.data
                for (let i = 0; i<resData.length; i++){
                    if (resData[i].like_num > 1000){
                        resData[i].like_num = app.numChange(resData[i].like_num)
                    }
                }
                let videoArrALL = that.data.videoArr.concat(resData)
                that.setData({
                    videoArr: videoArrALL,
                    totalPage: response.data.data.totalPage
                })
            } else {
                wx.showModal({
                    title: '温馨提示',
                    content: response.data.message,
                    showCancel: false
                })
            }
        })
    },
    // 下拉加载更多
    onReachBottom: function() {
        this.data.currentPage++
        if (this.data.currentPage > this.data.totalPage ) return false
        this.getVideolikeList(this.data.currentPage)
    }
})